package com.cdx.dxmall.common.valid;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.HashSet;
import java.util.Set;

/**
 * 自定义校验器
 * @author chedx
 * @data 2021/1/8 10:23 PM
 */
public class ListValueConstraintValidator implements ConstraintValidator<ListValue,Integer> {

    Set<Integer> set = new HashSet<>();

    /***
     *  初始的时候
     * @param listValue 获得注解信息
     */
    @Override
    public void initialize(ListValue listValue) {
        int[] vals = listValue.vals();
        for (int val : vals) {
            set.add(val);
        }
    }

    /***
     *
     * @param integer 被注解标记的值
     * @param constraintValidatorContext
     * @return
     */
    @Override
    public boolean isValid(Integer integer, ConstraintValidatorContext constraintValidatorContext) {
        return set.contains(integer);
    }
}
